play <- function() {
  get_symbols <- function() {
    wheel <- c("DD", "7", "BBB", "BB", "B", "C", "0")
    return(sample(wheel, size = 3, replace = TRUE, prob = c(0.03, 0.03, 0.06, 0.1, 0.25, 0.01, 0.4)))
  }

  symbols <- get_symbols()
  print(symbols)

  same <- all(symbols == symbols[1])
  bar <- all(symbols %in% c("B", "BB", "BBB"))

  if (same) {
    # 找到对应的中奖金额
    payout <- c("D" = 100, "7" = 80, "BBB" = 40, "BB" = 25, "B" = 10, "C" = 10)
    prize <- unname(payout[symbols[1]])
  } else if (bar) {
    prize <- 5
  } else {
    # 计算樱桃的数量
    cherry <- sum(symbols == "C")
    prize <- c(0, 2, 5)[cherry + 1]
  }

  # 计算钻石的数量
  diamond <- sum(symbols == "DD")
  prize <- prize + (2 * diamond)

  return(prize)
}

plays_till_broke <- function() {
  money <- 100  # 初始金额设为100元
  plays <- 0    # 游戏次数初始化为0

  while (money > 0) {  # 当玩家还有钱时继续游戏
    prize <- play()  # 进行一次游戏并获得奖金
    if (is.na(prize) || prize < 0) {
      prize <- 0  # 如果奖金是NA或负数，设置奖金为0
    }
    money <- money - 1 + prize  # 更新玩家的钱数，每次游戏花费1元
    plays <- plays + 1  # 游戏次数加1

    if (money < 0) {
      money <- 0  # 确保钱数不会变成负数
    }
  }

  return(plays)  # 返回游戏次数
}

print(plays_till_broke())
